<?php

namespace apiend\modules\dynamic\models;

use Yii;
use yii\data\ActiveDataProvider;

/**
 * This is the model class for table "{{%dynamic}}".
 *
 * @property int $id
 * @property string $user_id 用户ID
 * @property string $content
 * @property int $direction 动态类型：0主动动态，1被动动态
 * @property int $retweet_id 转发动态原始ID
 * @property int $status
 * @property string $created_at
 * @property string $updated_at
 * @property string $deleted_at
 */
class DynamicSearch extends \common\models\dynamic\Dynamic {

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params = []) {
        $query = static::find()->where([
            'status' => static::STATUS_ACTIVE,
            'deleted_at' => 0,
        ]);
        $query->with(['user', 'likes', 'comments', 'gallery']);

        if (isset($params['area']) && $params['area']) {
            //地区检索
            $query->andFilterWhere(['district' => $params['area']]);
        }

        $query->orderBy(['created_at' => SORT_DESC]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        return $dataProvider;
    }

    public function fields() {
        return [
            'id' => function() {
                return (string) $this->id;
            },
            'user',
            'content',
            'thumbs',
            'pics',
            'time' => function() {
                return (string) $this->created_at;
            },
            'liked',
            'like_num' => 'likeCount',
            'likes' => 'topLikes',
            'comments',
        ];
    }

    public function extraFields() {
        parent::extraFields();
    }

}
